package com.gs.data.convert.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串工具类
 *
 * @author: guoshuai
 * @date: 2024/05/11 09:59
 **/
@Slf4j
public class StringUtil extends com.gs.common.tool.utils.StringUtil {


    /**
     * 拼接单引号串
     * @param str    "1delimiter2delimiter3"
     * @param delimiter    分割符
     * @date 2025/10/15 09:39
     * @return  '1','2','3'
     **/
    public static String joinSingleQuot(String str, String delimiter) {
        if (isEmpty(str)) {
            return null;
        }
        return Arrays.stream(str.split(delimiter)).map(d -> String.format("'%s'", d.trim())).distinct().collect(Collectors.joining(","));
    }


    /**
     * 拼接双号串
     * @param str    "1delimiter2delimiter3"
     * @param delimiter    分割符
     * @date 2025/10/15 09:39
     * @return  "1","2","3"
     **/
    public static String joinDoubleQuot(String str, String delimiter) {
        if (isEmpty(str)) {
            return null;
        }
        return Arrays.stream(str.split(delimiter)).map(d -> String.format("\"%s\"", d.trim())).distinct().collect(Collectors.joining(","));
    }

    /**
     * 单引号集合->双引号集合
     *
     * @param val "'1','2','3'"
     * @date 2024/05/11 10:10
     **/
    public static List<String> singleQuotToDouble(String val, String delimiter) {
        if (isEmpty(val)) {
            return Lists.newArrayList();
        }
        List<String> valList = Lists.newArrayList(val.replaceAll("'", "").split(delimiter));
        return valList.stream().map(d -> String.format("\"%s\"", d.trim())).collect(Collectors.toList());
    }


    /**
     * 双引号集合->单引号
     *
     * @param val "'1','2','3'"
     * @date 2024/05/11 10:10
     **/
    public static String doubleQuotToSingle(String val, String delimiter) {
        if (isEmpty(val)) {
            return null;
        }
        String newStr = val.replaceAll("\"", "");
        return Arrays.stream(newStr.split(delimiter)).map(d -> String.format("'%s'", d.trim())).distinct().collect(Collectors.joining(","));
    }
}
